package com.android.basiclib.base;


import android.os.Bundle;
import androidx.annotation.Nullable;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProviders;
import com.android.basiclib.entity.LoadAction;
import com.android.basiclib.receiver.ConnectivityReceiver;
import com.android.basiclib.uitls.TUtil;
import com.android.basiclib.uitls.ToastUtils;
import com.android.basiclib.view.LoadingDialogManager;


/**
 * 兼容MVVM和MVP模式的基类
 */

public abstract class BaseActivity<P extends BaseViewModel> extends AbsActivity implements
        ConnectivityReceiver.ConnectivityReceiverListener {

    protected P mViewModel;

    /**
     * 初始化ViewModel去执行它的方法获取数据
     */
    protected P createViewModel() {
        Object o = TUtil.getInstance(this, 0);
        if (o != null) {
            try {
                return ViewModelProviders.of(this, new AutoViewModelFactory(this)).get((Class<P>) o);
            } catch (Exception e) {
                e.printStackTrace();
            }
            return null;
        } else {
            return null;
        }
    }


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        mViewModel = createViewModel();
        //观察网络数据状态
        if (mViewModel != null) {
            mViewModel.getActionLiveData().observe(this, stateObserver);
        }

        super.onCreate(savedInstanceState);

        //观察其他的数据
        dataObserver();
    }

    protected abstract void dataObserver();

    /**
     * 只映射了onDestroy方法
     */
    @Override
    protected void onDestroy() {
        super.onDestroy();
        LoadingDialogManager.get().dismissLoading();

        //解绑，取消Presenter订阅
        if (mViewModel != null)
            mViewModel.cleanTask();
    }

    // ================== 网络状态的监听 ======================

    protected Observer stateObserver = new Observer<LoadAction>() {
        @Override
        public void onChanged(@Nullable LoadAction loadAction) {
            if (loadAction != null) {
                if (LoadAction.STATE_NORMAL == loadAction.action) {
                    showStateNormal();
                } else if (LoadAction.STATE_ERROR == loadAction.action) {
                    showStateError(loadAction.message);
                } else if (LoadAction.STATE_SUCCESS == loadAction.action) {
                    showStateSuccess();
                } else if (LoadAction.STATE_LOADING == loadAction.action) {
                    showStateLoading(loadAction.message);
                } else if (LoadAction.STATE_NO_DATA == loadAction.action) {
                    showStateNoData();
                }
            }
        }
    };

    protected void showStateNormal() {
    }

    protected void showStateError(String message) {
        LoadingDialogManager.get().dismissLoading();
        ToastUtils.showFailText(mActivity, message);
    }

    protected void showStateSuccess() {
        LoadingDialogManager.get().dismissLoading();
    }

    protected void showStateLoading(String message) {
        LoadingDialogManager.get().showLoading(this);
    }

    protected void showStateNoData() {
        LoadingDialogManager.get().dismissLoading();
    }

}
